<template>
	<view class="main">
		<h2>相关信息</h2>
		<view class="form">
			<view class="item">
				<view class="msg">
					<span>*</span>
					联系人
				</view>
				<view class="input">
					<input v-model="data.realname" type="text" value="" />
				</view>
			</view>
			<view class="item">
				<view class="msg">
					<span>*</span>
					手机号
				</view>
				<view class="input">
					<input v-model="data.mobile" type="text" value="" />
				</view>
			</view>
			<view class="item">
				<view class="msg">
					<span>*</span>
					所在地区
				</view>
				<view class="seleinput">
					<view class="sele" @tap="show1 = true,allAdd = false">
						<span>{{ seleAdd }}</span>
						<!-- <image src="../static/img/down.png" mode=""></image> -->
					</view>
				</view>
			</view>
			<view class="item">
				<view class="msg">
					<span>*</span>
					详细地址
				</view>
				<view class="input">
					<u--textarea v-model="data.address" height="80" maxlength="500" border="none"></u--textarea>
				</view>
			</view>
			<view class="moren">
				<view class="msg">
					设为默认地址
				</view>
				<view class="input">
					<image @tap="data.status = 0" v-if="data.status == 1" src="@/static/img/Group.png" mode=""></image>
					<image @tap="data.status = 1" v-else src="@/static/img/noGroup.png" mode=""></image>
				</view>
			</view>
			<view class="cottom_yinsi">
				<radio style="transform:scale(0.7)" color="#EA504F" @click="current = !current" :checked="current"/>
				<text>我已阅读、理解并同意</text> <text @click="routerTo('/pageB/text?name=Agreement&title=用户协议')">《用户协议》</text><text @click="routerTo('/pageB/text?name=Privacy&title=隐私政策')">《隐私协议》</text>
			</view>
		</view>
		<u-picker :closeOnClickOverlay="true" @close="show1 = false" :show="show1" :columns="columns1"
			keyName="name" @confirm="confirm1"></u-picker>
		<u-picker :cancelText="cancelText" :show="show2" :columns="columns2"
			keyName="name" @confirm="confirm2"></u-picker>
		<u-picker :cancelText="cancelText" :show="show3" :columns="columns3"
			keyName="name" @confirm="confirm3"></u-picker>
		<u-picker :cancelText="cancelText" :show="show4" closeOnClickOverlay @close="show4 = false" :columns="columns4"
			keyName="name" @confirm="confirm4"></u-picker>
		<view class="bottom">
			<view class="btn" @tap="edit">保存</view>
		</view>
	</view>
</template>

<script>
	import {
		District,
		address,
		read
	} from '@/api/index.js';
	export default {
		data() {
			return {
				value1: '',
				addressList: [],
				columns1: [],
				columns2: [],
				columns3: [],
				columns4: [],
				show1: false,
				show2: false,
				show3: false,
				show4: false,
				data: {
					mobile: '',
					area: '',
					address: '',
					realname: '',
					status: 0,
					id: ''
				},
				seleAdd: '请选择省 市 区/县 街道/乡镇',
				type: '',
				cancelText:null,
				allAdd:false,
				current: false
			};
		},
		onLoad(e) {
			if (e.id) {
				this.data.id = e.id
				this.detail()
			}
			this.getList();
		},
		methods: {
			isPhone(phone) {
				const reg = /^1\d{10}$|^(0\d{2,3}-?|\(0\d{2,3}\))?[1-9]\d{4,7}(-\d{1,8})?$/
				return reg.test(phone);
			},
			async edit() {
				if (!this.data.mobile) {
					uni.showToast({
						title: '请输入手机号',
						icon: 'none'
					})
					return
				}
				if (!this.data.address) {
					uni.showToast({
						title: '请输入详细收货地址',
						icon: 'none'
					})
					return
				}
				if (!this.data.realname) {
					uni.showToast({
						title: '请输入联系人',
						icon: 'none'
					})
					return
				}
				if (!this.isPhone(this.data.mobile)) {
					uni.showToast({
						title: '请输入正确的手机号',
						icon: 'none'
					})
					return
				}
				console.log(this.data)
				if (!this.allAdd) {
					uni.showToast({
						title: '请完善所在地区',
						icon: 'none'
					})
					return
				}
				if (!this.current) {
					uni.showToast({
						title: '请选中用户协议和隐私协议',
						icon: 'none'
					})
					return
				}
				let res = await address(this.data)
				if (res.code == 1) {
					uni.showToast({
						title: '保存成功',
						icon: 'none'
					})
					setTimeout(() => {
						uni.navigateBack()
					}, 1000)
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				}
			},
			async detail() {
				let res = await read({
					id: this.data.id
				})
				this.data.realname = res.data.realname
				this.data.mobile = res.data.mobile
				this.data.address = res.data.address
				this.data.area = res.data.area
				this.data.status = res.data.status
				this.seleAdd = `${res.data.province} ${res.data.city} ${res.data.district} ${res.data.town}`
				// if(res.data.town){
				// 	this.allAdd = true
				// }
			},
			 getList() {
				// let res = await District();
				this.addressList = uni.getStorageSync('addressList');
				const arr = this.addressList.map((item) => {
					return {
						name: item.name,
						cid: item.cid,
						son: item.son
					};
				});
				arr.pop()
				this.columns1 = [arr];
			},
			confirm1(e) {
				if (e.value[0].son.length == 0) {
					this.data.area = e.value[0].cid;
					this.show1 = false;
				} else {
					const arr = e.value[0].son.map((item) => {
						return {
							name: item.name,
							cid: item.cid,
							son: item.son
						};
					});
					this.columns2 = [arr];
					this.show2 = true;
					this.show1 = false;
				}
				this.seleAdd = e.value[0].name;
			},
			confirm2(e) {
				if (e.value[0].son.length == 0) {
					this.data.area = e.value[0].cid;
					this.show2 = false;
				} else {
					const arr = e.value[0].son.map((item) => {
						return {
							name: item.name,
							cid: item.cid,
							son: item.son
						};
					});
					this.columns3 = [arr];
					this.show3 = true;
					this.show2 = false;
				}
				this.seleAdd = this.seleAdd + ' ' + e.value[0].name;
			},
			confirm3(e) {
				if (e.value[0].son.length == 0) {
					this.data.area = e.value[0].cid;
					this.show2 = false;
				} else {
					const arr = e.value[0].son.map((item) => {
						return {
							name: item.name,
							cid: item.cid,
							son: item.son
						};
					});
					this.columns4 = [arr];
					this.show4 = true;
					this.show3 = false;
				}
				this.seleAdd = this.seleAdd + ' ' + e.value[0].name;
			},
			confirm4(e){
				this.seleAdd = this.seleAdd + ' ' + e.value[0].name;
				this.data.area = e.value[0].cid;
				this.show4 = false;
				this.allAdd = true
			},
			routerTo(url) {
				uni.navigateTo({
					url: url
				})
			}
		}
	};
</script>

<style lang="scss" scoped>
	.main {
		min-height: calc(100vh - 88rpx);
		padding: 30rpx;
		background-color: #f2f2f2;

		h2 {
			font-size: 32rpx;
			color: #232323;
			font-weight: 700;
			margin-bottom: 40rpx;
		}

		.form {
			.moren {
				display: flex;
				align-items: center;

				.msg {
					font-size: 28rpx;
					color: #000;
					margin-right: 20rpx;
				}

				.input {
					image {
						width: 40rpx;
						height: 40rpx;
					}
				}
			}

			.item {
				display: flex;
				align-items: center;
				margin-bottom: 40rpx;

				.msg {
					width: 20%;
					font-size: 28rpx;
					color: #000;

					span {
						color: #fa3b3a;
					}
				}

				.input {
					width: 80%;

					input {
						padding: 0 10rpx;
						height: 80rpx;
						background-color: #fff;
						border-radius: 20rpx;
						color: #000;
					}

					::v-deep.u-textarea {
						background-color: #fff;
						border-radius: 20rpx;
						color: #000;
					}
				}

				.seleinput {
					width: 80%;

					.sele {
						padding: 0 10rpx;
						min-height: 80rpx;
						border-radius: 20rpx;
						width: 100%;
						background-color: #fff;
						color: #000;
						display: flex;
						align-items: center;
						justify-content: space-between;

						image {
							width: 24rpx;
							height: 24rpx;
							margin-left: 10rpx;
						}
					}
				}
			}
		}

		.bottom {
			position: fixed;
			bottom: 30rpx;
			left: 0;
			width: 100%;
			padding: 20rpx 30rpx;
			background-color: #fff;

			.btn {
				width: 100%;
				padding: 24rpx 0;
				text-align: center;
				color: #fff;
				background-color: #EA504F;
				border-radius: 200rpx;
			}
		}
		
		.cottom_yinsi {
			font-size: 20rpx;
			margin: 50rpx 0rpx 30rpx 0rpx;
			color: gray;
			display: flex;
			align-items: center;
		
			image {
				display: inline-block;
				width: 40rpx;
				height: 40rpx;
				position: relative;
				top: 11rpx;
				padding-right: 10rpx;
			}
		
			text {
				font-size: 25rpx;
				color: #666;
			}
		}
	}
</style>